package kr.saeildamil.androidforstudy2.ch20;

import kr.saeildamil.androidforstudy2.R;
import kr.saeildamil.androidforstudy2.ch20.common.CounterFragment01;
import kr.saeildamil.androidforstudy2.ch20.common.CounterFragment02;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class FragmentManagerTest extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch20_fragmentmanagementtest);
		
	}
	
	public void mOnClick(View v){
		FragmentManager fm = getFragmentManager();
		Fragment fragment = fm.findFragmentById(R.id.ch20_fragmentmanagementtest_frame);
		
		switch (v.getId()) {
		case R.id.ch20_fragmentmanagementtest_addbtn:
			if(fragment==null){
				FragmentTransaction ft = fm.beginTransaction();
				CounterFragment01 cf = new CounterFragment01();
				ft.add(R.id.ch20_fragmentmanagementtest_frame, cf, "Counter").commit();
				// fm.beginTransaction().add(R.id.ch20_fragmentmanagementtest_frame, cf, "Counter").commit();
			}else{
				Toast.makeText(this, "Fragment is already exist.", Toast.LENGTH_SHORT).show();
			}
			break;
			
		case R.id.ch20_fragmentmanagementtest_removebtn:
			if(fragment==null){
				Toast.makeText(this, "There's no fragment to remove.", Toast.LENGTH_SHORT).show();
			}else{
				fm.beginTransaction().remove(fragment).commit();
			}
			break;
			
		case R.id.ch20_fragmentmanagementtest_replacebtn:
			if(fragment==null){
				Toast.makeText(this, "There's no fragment to replace.", Toast.LENGTH_SHORT).show();
			}else{
				if(fragment.getTag().equals("Counter")){
					fm.beginTransaction().replace(R.id.ch20_fragmentmanagementtest_frame, new TextFragment(), "text").commit();
				}else{
					fm.beginTransaction().replace(R.id.ch20_fragmentmanagementtest_frame, new CounterFragment02(), "Counter").commit();
				}
			}
			break;
			
		case R.id.ch20_fragmentmanagementtest_hidebtn:
			if(fragment==null){
				Toast.makeText(this, "There's no fragment.", Toast.LENGTH_SHORT).show();
			}else{
				if(fragment.isHidden()){
					fm.beginTransaction().show(fragment).commit();
				}else{
					fm.beginTransaction().hide(fragment).commit();
				}
			}
			break;
		}
	}
}
